package com.amumu.drama.portal.modules.order.controller;

import com.amumu.drama.common.base.api.CommonPage;
import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.common.base.dto.req.IdReq;
import com.amumu.drama.common.exception.Asserts;
import com.amumu.drama.common.modules.order.dto.GenerateOrderResult;
import com.amumu.drama.common.modules.order.dto.OmsOrderDetail;
import com.amumu.drama.common.modules.order.dto.OrderParam;
import com.amumu.drama.common.modules.order.dto.req.OrderQuery;
import com.amumu.drama.common.modules.order.dto.req.PayOrderReq;
import com.amumu.drama.common.modules.order.dto.req.ValidateInvitationCodeReq;
import com.amumu.drama.common.modules.order.dto.resp.MemberOrderListResp;
import com.amumu.drama.common.modules.order.dto.resp.ValidateInvitationCodeResp;
import com.amumu.drama.common.modules.order.mapping.MemberOrderDetailListRespMapping;
import com.amumu.drama.common.modules.order.service.OmsPortalOrderService;
import com.amumu.drama.portal.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理Controller
 * Created by macro on 2018/8/30.
 */

@Api(tags = "订单")
@RequestMapping("/order")
@RestController
public class OmsPortalOrderController {
    @Autowired
    private OmsPortalOrderService portalOrderService;
    @Autowired
    private AuthService authService;

    @ApiOperation("微丸子-首充状态")
    @GetMapping("/queryFirstOrderStatus")
    public CommonResult<Boolean> queryFirstOrderStatus() {
        return CommonResult.success(portalOrderService.queryFirstOrderStatus(authService.getCurrentMember()));
    }

    @ApiOperation("微丸子-用户邀请码提前校验")
    @PostMapping("/validateInvitationCode")
    public CommonResult<ValidateInvitationCodeResp> validateInvitationCode(@RequestBody @Validated ValidateInvitationCodeReq request) {
        ValidateInvitationCodeResp resp = portalOrderService.validateInvitationCode(request, authService.getCurrentMember());
        resp.setInvitationAnchor(null);
        return CommonResult.success(resp);
    }

    @ApiOperation("微丸子-生成订单")
    @PostMapping(value = "/generateOrder")
    public CommonResult<GenerateOrderResult> generateOrder(@RequestBody @Validated OrderParam orderParam) {
        if (orderParam.getFeature() == null || orderParam.getFeature().isEmpty()) {
            Asserts.fail("游戏账户信息不能为空");
        }
        GenerateOrderResult result = portalOrderService.generateOrder(orderParam, authService.getCurrentMember());
        return CommonResult.success(result, "下单成功");
    }

    @ApiOperation("微丸子-去支付")
    @PostMapping(value = "/payOrder")
    public CommonResult<GenerateOrderResult> payOrder(@RequestBody @Validated PayOrderReq request) {
        return CommonResult.success(portalOrderService.payOrder(request, authService.getCurrentMember()));
    }

    @ApiOperation("微丸子-按状态分页获取用户订单列表-新版 订单状态：All->全部；WaitingPay->待付款；InProgress->进行中；Finished->已完成")
    @PostMapping(value = "/queryOrderList")
    public CommonResult<CommonPage<MemberOrderListResp>> queryOrderList(@RequestBody @Validated OrderQuery request) {
        return CommonResult.success(portalOrderService.queryOrderList(request, authService.getCurrentMember()));
    }

    @ApiOperation("微丸子-取消订单（关闭订单）")
    @PostMapping(value = "/cancelOrder")
    public CommonResult cancelOrder(@RequestBody @Validated IdReq request) {
        portalOrderService.cancelOrder(request.getId(), authService.getCurrentMember());
        return CommonResult.success();
    }

    @ApiOperation("微丸子-根据ID获取订单详情")
    @RequestMapping(value = "/detail/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MemberOrderListResp> detail(@PathVariable Long orderId) {
        return CommonResult.success(portalOrderService.orderDetail(orderId));
    }

    @ApiOperation("微丸子-按状态分页获取用户订单列表-（旧，尽量丢器不用）")
    @ApiImplicitParam(name = "status", value = "订单状态：All->全部；WaitingPay->待付款；InProgress->进行中；Finished->已完成",
            defaultValue = "All", allowableValues = "All,WaitingPay,InProgress,Finished", paramType = "query", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<MemberOrderListResp>> list(@RequestParam(required = false, defaultValue = "All") String status,
                                                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                              @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        CommonPage<OmsOrderDetail> orderPage = portalOrderService.list(status, pageNum, pageSize, authService.getCurrentMember());
        List<MemberOrderListResp> memberOrderListRespList = MemberOrderDetailListRespMapping.INSTANCE.convertTo(orderPage.getList());
        CommonPage<MemberOrderListResp> resp = new CommonPage<>();
        resp.setList(memberOrderListRespList);
        resp.setPageNum(orderPage.getPageNum());
        resp.setPageSize(orderPage.getPageSize());
        resp.setTotal(orderPage.getTotal());
        resp.setTotalPage(orderPage.getTotalPage());
        return CommonResult.success(resp);
    }


    //    @ApiOperation("根据购物车信息生成确认单")
//    @RequestMapping(value = "/generateConfirmOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult<ConfirmOrderResult> generateConfirmOrder(@RequestBody List<Long> cartIds) {
//        ConfirmOrderResult confirmOrderResult = portalOrderService.generateConfirmOrder(cartIds, authService.getCurrentMember());
//        return CommonResult.success(confirmOrderResult);
//    }

//    @ApiOperation("用户支付成功的回调")
//    @RequestMapping(value = "/paySuccess", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult paySuccess(@RequestParam Long orderId, @RequestParam Integer payType) {
//        Integer count = portalOrderService.paySuccess(orderId, payType);
//        return CommonResult.success(count, "支付成功");
//    }

//    @ApiOperation("自动取消超时订单")
//    @RequestMapping(value = "/cancelTimeOutOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult cancelTimeOutOrder() {
//        portalOrderService.cancelTimeOutOrder();
//        return CommonResult.success(null);
//    }
//
//    @ApiOperation("取消单个超时订单")
//    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult cancelOrder(Long orderId) {
//        portalOrderService.sendDelayMessageCancelOrder(orderId);
//        return CommonResult.success(null);
//    }


    //todo 处理用户未支付取消订单逻辑
//    @ApiOperation("用户取消订单（还未处理）")
//    @RequestMapping(value = "/cancelUserOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult cancelUserOrder(Long orderId) {
//        portalOrderService.cancelOrder(orderId);
//        return CommonResult.success(null);
//    }

//    @ApiOperation("用户确认收货")
//    @RequestMapping(value = "/confirmReceiveOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult confirmReceiveOrder(Long orderId) {
//        portalOrderService.confirmReceiveOrder(orderId, authService.getCurrentMember());
//        return CommonResult.success(null);
//    }

//    @ApiOperation("用户删除订单")
//    @RequestMapping(value = "/deleteOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult deleteOrder(Long orderId) {
//        portalOrderService.deleteOrder(orderId, authService.getCurrentMember());
//        return CommonResult.success(null);
//    }
}
